package com.hello.Launcher;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.hello.MainMenuScreen;
import com.hello.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.text.format.Time;
import android.widget.Toast;

public class AutoLauncherService extends Service {
	
	
	@Override
	public IBinder onBind(Intent intent) {

		return null;

	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		Intent serviceIntent = new Intent();
		serviceIntent.setAction("com.hello.Launcher.AutoLauncherService");
		stopService(serviceIntent);
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		WaitForTheFirstTime();
	}


	//restart every 24 hours
	private void StartRecursevly(){
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {			
			public void run() {
				Intent locationService = new Intent();
				locationService.setAction("com.hello.Launcher.LocationLauncherService");
				startService(locationService);
				if (ShouldRunToday()){					
					Intent intent = new Intent(AutoLauncherService.this,LaunchAppFromGame.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					AutoLauncherService.this.startActivity(intent);
				}
				StartRecursevly();
			} 
		}, 24 * 60 * 60 * 1000);
	}


	private void WaitForTheFirstTime(){
		Calendar cur_cal = new GregorianCalendar();
		cur_cal.setTimeInMillis(System.currentTimeMillis());
		Calendar cal_one = getStartTime();
		long delta =  cal_one.getTimeInMillis() - cur_cal.getTimeInMillis();
		if (delta < 0) delta += 60 * 60 * 1000 * 24;
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {			
			public void run() {
				Intent locationService = new Intent();
				locationService.setAction("com.hello.Launcher.LocationLauncherService");
				startService(locationService);
				if (ShouldRunToday()){
					Intent intent = new Intent(AutoLauncherService.this,LaunchAppFromGame.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					AutoLauncherService.this.startActivity(intent);
				}
				StartRecursevly();
			}
		},delta);
	}

	protected boolean ShouldRunToday() {
		boolean retval = true;
		Time t = new Time();
		t.setToNow();
		SharedPreferences pref = getSharedPreferences("com.hello_preferences", MODE_WORLD_READABLE);
		boolean[] days = new boolean[7];
		days[0] = pref.getBoolean("prefsSunday", false);
		days[1] = pref.getBoolean("prefsMonday", false);
		days[2] = pref.getBoolean("prefsTuesday", false);
		days[3] = pref.getBoolean("prefsWednesday", false);
		days[4] = pref.getBoolean("prefsThursday", true);
		days[5] = pref.getBoolean("prefsFriday", true);
		days[6] = pref.getBoolean("prefsSaturday", true);
		return days[t.weekDay];
	}

	private Calendar getStartTime() {
		Calendar cur_cal = new GregorianCalendar();
		cur_cal.setTimeInMillis(System.currentTimeMillis());
		Calendar cal_one = new GregorianCalendar();
		SharedPreferences pref = getSharedPreferences("com.hello_preferences", MODE_WORLD_READABLE);
		String time = pref.getString("prefsTime","23:00");
		String[]splitTime = time.split(":");
		int hour = Integer.parseInt(splitTime[0]);
		int minutes = Integer.parseInt(splitTime[1]);
		cal_one.set(Calendar.HOUR_OF_DAY, hour);
		cal_one.set(Calendar.MINUTE,minutes);
		cal_one.set(Calendar.SECOND, 0);
		cal_one.set(Calendar.MILLISECOND,0);
		cal_one.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
		cal_one.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
		return cal_one;
	}
}
